package edu.upn.sigemfi.sac.bc;

import edu.upn.sigemfi.seg.bc.PersonaJuridicaFacadeLocal;
import edu.upn.sigemfi.seg.bc.PersonaNaturalFacadeLocal;
import edu.upn.sigemfi.seg.be.PersonaJuridica;
import edu.upn.sigemfi.seg.be.PersonaNatural;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class PersonaDelegate {

    private PersonaNaturalFacadeLocal localPN;
    private PersonaJuridicaFacadeLocal localPJ;

    public PersonaDelegate() {
        localPN = lookupPersonaNaturalFacade();
        localPJ = lookupPersonaJuridicaFacade();
    }

    public PersonaNatural buscarPersonaPorDNI(String dni) throws Exception{
        PersonaNatural persona = null;

        try {
            persona = localPN.findByDni(dni);
        } catch (Exception ex) {
            localPN = lookupPersonaNaturalFacade();
            throw new Exception("Buscar Persona por Dni... " + ex.getMessage());
        }

        
        return persona;
    }


    public PersonaJuridica buscarPersonaPorRUC(String ruc) throws Exception{
        PersonaJuridica persona = null;

//        try {
//            persona = localPJ.findByRuc(ruc);
//        } catch (Exception ex) {
//            localPJ = lookupPersonaJuridicaFacade();
//            throw new Exception("Buscar Persona por Ruc... " + ex.getMessage());
//        }

        
        return persona;
    }

    private PersonaNaturalFacadeLocal lookupPersonaNaturalFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaNaturalFacadeLocal) c.lookup("java:comp/env/PersonaNaturalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private PersonaJuridicaFacadeLocal lookupPersonaJuridicaFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaJuridicaFacadeLocal) c.lookup("java:comp/env/PersonaJuridicaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
